package hamilton_bubbles_alpha;

import Geometry.Circle;
import Geometry.Picture;
import Geometry.Swarm;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.PointF;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.example.hamilton_bubbles_alpha.R;

public class MainActivity extends Activity {

	private SwarmView sv;
	private SeekBar createSpeedSB;
	private Button stopBtn;
	private Button goBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		createSpeedSB = (SeekBar) findViewById(R.id.createSpeedSB);
		createSpeedSB.setOnSeekBarChangeListener(speedListener);

		stopBtnListener sbl = new stopBtnListener();
		goBtnListener gbl = new goBtnListener();
		stopBtn = (Button) findViewById(R.id.stopBtn);
		goBtn = (Button) findViewById(R.id.goBtn);
		stopBtn.setOnClickListener(sbl);
		goBtn.setOnClickListener(gbl);

		sv = (SwarmView) findViewById(R.id.swarmView1); // oops, bad id
		DrawInitialBubbles dib = new DrawInitialBubbles();
		sv.setOnSizeChangedListener(dib);
		// don't start the bubble creator here
		// the width and height are 0
		RemoveBubbleListener rbl = new RemoveBubbleListener();
		sv.setOnTouchListener(rbl);
	}

	private class DrawInitialBubbles implements OnSizeChangedListener {
		/**
		 * This method executes only after the view has finally become visually
		 * valid: it has a real width and height.
		 */
		@Override
		public void onSizeChanged(int w, int h, int oldw, int oldh) {

			stopBtnListener sgl = new stopBtnListener();
			stopBtn = (Button) findViewById(R.id.stopBtn);
			stopBtn.setOnClickListener(sgl);

			goBtnListener gbl = new goBtnListener();
			goBtn = (Button) findViewById(R.id.goBtn);
			goBtn.setOnClickListener(gbl);

			// draw the initial set of bubbles
			Swarm sw = new Swarm();
			Resources res = getResources(); // inherit getResources from
			int color = res.getColor(R.color.bubbleColor);
			int numberOfCircles = res
					.getInteger(R.integer.initialNumberOfBubbles);
			int radius = res.getInteger(R.integer.bubbleRadius);
			for (int i = 0; i < numberOfCircles; i++) {
				float x = (float) Math.random() * w;
				float y = (float) Math.random() * h;
				PointF center = new PointF(x, y);
				Circle c = new Circle(color, center, radius);
				sw.addPicture(c);
			}
			sv.setTheSwarm(sw);
			startMakingBubbles();
			sv.invalidate();
		}
	}

	private boolean makeBubblesIsRunning;

	/**
	 * This stops the bubble creation loop and lets the thread terminate
	 * 
	 * @author hamilton
	 */

	private class stopBtnListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			stopMakingBubbles();
		}
	}

	private class goBtnListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			startMakingBubbles();
		}
	}

	private void stopMakingBubbles() {
		makeBubblesIsRunning = false;
	}

	private void startMakingBubbles() {
		if (!makeBubblesIsRunning) {
			makeBubblesIsRunning = true;
			Runnable rbl = new MakeBubbles();
			Thread trbl = new Thread(rbl);
			trbl.start();
		}
	}

	private class MakeBubbles implements Runnable {

		@Override
		public void run() {
			while (makeBubblesIsRunning) {
				// create a bubble
				// get resources which give us the color and radius
				Resources res = getResources(); // inherit getResources from
				int color = res.getColor(R.color.bubbleColor);
				int radius = res.getInteger(R.integer.bubbleRadius);
				// get height and the width from the view (sv) to pick a random
				// point
				float x = (float) Math.random() * sv.getWidth();
				float y = (float) Math.random() * sv.getHeight();
				// create the circle
				PointF center = new PointF(x, y);
				Circle c = new Circle(color, center, radius);
				// add it to the swarm
				Swarm sw = sv.getTheSwarm();
				sw.addPicture(c);
				// sleep a while
				try {
					Thread.sleep(delay); // 200 milliseconds
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} // 200 milliseconds
				sv.postInvalidate();
			}
		}
	}

	private int delay = 500;
	/**
	 * Define and initialize a seek bar listener variable using an anonymous
	 * class
	 */
	private OnSeekBarChangeListener speedListener = new OnSeekBarChangeListener() {

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			// nothing
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			// nothing
		}

		/**
		 * Change the rate at which bubbles are created.
		 */
		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			// The parameter process is the same as the
			// 'createSpeedSB.getProgress().
			// since there is only one seek bar

			delay = (100 - progress) * 4 + 100; // ranges from 100 - 500
			// far left on the seek bar is slow
			// far right on the seek bar is fast
		}
	};

	private class RemoveBubbleListener implements OnTouchListener {

		@Override
		public boolean onTouch(View v, MotionEvent event) {

			int action = event.getActionMasked();

			if (action == MotionEvent.ACTION_DOWN) {
				// Test if the touch is inside the picture
				float x = event.getX();
				float y = event.getY();
				Swarm theSwarm = sv.getTheSwarm();
				Picture thePic = theSwarm.findContaining(x, y);

				if (thePic != null) {
					theSwarm.removePicture(thePic);
					sv.invalidate();
				} else {
					// do nothing
				}
			}
			return true;
		}
	}
}
